package org.cmg.study.spring.config;

import org.springframework.format.Formatter;
import org.springframework.stereotype.Component;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;

/**
 * @author CMG
 * @version 1.0
 * @cLassName MyFormatter
 * @description
 * @date 2021/9/6 9:37
 **/
@Component
public class MyFormatter implements Formatter<Date> {
    SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
    @Override
    public Date parse(String s, Locale locale) throws ParseException {
        //Formatter只能对字符串转换
        return format.parse(s);
    }

    @Override
    public String print(Date date, Locale locale) {
        return format.format(date);
    }
}
